package com.github.postsanf.yinian.slider.library.Tricks;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import java.util.List;

/**
 * Created by Administrator on 2016/8/12.
 */
public class ImageAdapter3 extends PagerAdapter {
    private List<ImageView> viewlist;

    public ImageAdapter3(List<ImageView> viewlist) {
        this.viewlist = viewlist;
    }

    @Override
    public int getCount() {
        //设置成最大，使用户看不到边界
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position,
                            Object object) {
        ((ViewPager) container).removeView(viewlist.get(position % viewlist.size()));
        View view = container.getChildAt(position);
        view = null;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ViewParent parent = viewlist.get(position % viewlist.size()).getParent();
        if (parent != null) {
            ((ViewPager) container).removeView(viewlist.get(position % viewlist.size()));
        }
        container.addView(viewlist.get(position % viewlist.size()));
        return viewlist.get(position % viewlist.size());
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }
}
